
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.role.struts.actions;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.application.role.entity.Role;
import com.application.role.struts.form.RoleForm;
import com.application.role.service.RoleService;
import com.application.role.commons.Constants;

/**
 * 
 * Description:RoleAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class RoleAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private RoleService roleService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Role role;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getRolePartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchRole" + "开始");
		RoleForm role = (RoleForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List roleList = null;
		resultSize = roleService.getAllCountRole(role);
		pageBean = PageBean.getPageBean("roleTab", resultSize, request);
		roleList = roleService.searchRole(role,
				pageBean);
		request.setAttribute("roleList", roleList);

		log.debug("searchRole" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddRole(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddRoles");
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addRoles");
		Role role = this.getRole(form);
		role.setId(RandomKey.generateID_Long_19().toString());
		role.setCreateDate(new Date());

		this.roleService.insertObject(role);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteRole(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteRoles");
		RoleForm roleForm = (RoleForm) form;
		this.roleService.deleteRoles(roleForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateRole(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateRoles");
		RoleForm roleForm = (RoleForm) form;
		role = (Role) this.roleService.searchRole(
				roleForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, role);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateRole(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateRoles");
		Role role = this.getRole(form);
		role.setCreateDate(new Date());
		this.roleService.updateObject(role);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewRole");
		RoleForm roleForm = (RoleForm) form;
		role = (Role) this.roleService.searchRole(
				roleForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, role);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the RoleService
	 */
	public RoleService getRoleService() {
		return this.roleService;
	}

	/**
	 * @param RoleService
	 *            the RoleService to set
	 */
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * 获取Role实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Role
	 */
	public Role getRole(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		RoleForm roleForm = (RoleForm) form;
		role = new Role();
		BeanUtils.copyProperties(role, roleForm);
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public ActionForward creatTreeSelect(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/text;charset=UTF-8");
		// System.out.println(request.getParameter("pid"));
		RoleForm roleForm = new RoleForm();
		String vid = request.getParameter("treeid");
		int tjs = Integer.parseInt(request.getParameter("js"));
		Long pid = Long.parseLong(request.getParameter("pid"));
		List<Role> accList = roleService
				.searchRole(roleForm);
		String html = creatTreeSelect(accList, vid, tjs);
		java.io.PrintWriter out = response.getWriter();
		out.print(html);
		out.flush();
		out.close();
		return null;
	}

	public String creatTreeSelect(List<Role> accList, String vid, int js) {
		String html = "";
		for (int i = 0; i < accList.size(); i++) {
			Role role = (Role) accList.get(i);
			for (int j = 0; j < js; j++) {
				html = html + "<img src='images/line.gif'>";
			}
//			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
//					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + role.getId() + "');\" >"
//					+ "<img src='images/close1.gif' id='img" + vid + js + i
//					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + role.getId()
//					+ "');\" style=\"cursor:hand\">&nbsp;";

			html = html + "<a onclick=\"selectTrees('" + role.getId()
					+ "','" + role.getName()
					+ "')\" style=\"cursor:hand\">" + role.getName()
					+ "</a>";
			html = html + "<div id='" + vid + js + i
					+ "' ><span class='f1'>.</span></div>";
		}
		return html;
	}

}
